<?php
/**
 * @class Common 公有函数类
 * @constructor
 * @author Lynk Li
 */
class Common {
	/**
	 * 提示函数
	 * @param String $prompt 提示内容
	 */
	function fatal($prompt) {
		$s = "Click <a href='javascript:history.back(-1)' title='back'>Here</a> Back";
		die($prompt.$s);
	}
	
	/**
	 * 数据库语句字符串过滤，并执行SQL，返回ADODB结果集
	 * @param String $statement Sql语句
	 */
	function sql_escape($statement) {
		global $DB;
		$arg = array();
		for ($i = 1;  $i < func_num_args();  ++ $i) {
			$s = func_get_arg($i);
			array_push($arg, addslashes($s));
		}
	
		$sql = "";
		$offset = 0;
		$count = count($arg);
		while ($pos = strpos($statement, "?", $offset)) {
			$sql .= substr($statement, $offset, $pos-$offset);
			$sql .= "'" . array_shift($arg) . "'";
			$offset = $pos + 1;
			$count --;
		}
		$sql .= substr($statement, $offset);
	
		if ($count != 0) $this->fatal("mysql escape parameter count mismatch: $statement");
		
		return $DB->Execute($sql);
	}
	
	/**
	 * 初始化Smarty模版对象
	 */
	function template_init() {
		global $CONFIG;
		
		$smarty = new Smarty;
		$smarty->template_dir	= $CONFIG->template["dir"]."/".$CONFIG->lang;
		$smarty->compile_dir	= $CONFIG->template["compile"];
		$smarty->assign("config_lang",$CONFIG->lang);
		return $smarty;
	}
	
	/**
	 * get database table insert id
	 * @param String $table database table name
	 */
	function getInsertId($table) {
		global $CONFIG;
		$id = 0;
		if ($handle = @fopen($CONFIG->base_path . "queue/" . $table . ".txt", "rb")) {
			do {
				$data = fread($handle, 16);
				if (strlen($data) == 0) {
					break;
				}
				$id .= $data;
			} while(true);
			
			fclose ($handle);
		}
		
		$id = intval($id);
		
		if ($handle = @fopen($CONFIG->base_path . "queue/" . $table . ".txt", "w")) {
			if (!fwrite($handle, $id + 1)) {
				print "Can't write into file " . $CONFIG->base_path . "queue/" . $table . ".txt";
			}			
			fclose ($handle);
		}
		
		return $id;
	}
	
	/**
	 * get datas from database query's resultset
	 * @param ResultSet $rs database query's resultset
	 */
	function getDataFromResultSet($rs) {
		$a = array();
		while (!$rs->EOF) {
			$a[] = $rs->fields;
			$rs->MoveNext();
		}
		$rs->Close();
		return $a;
	}
	
	/**
	 * function getOptionsString
	 * 根据数组获取下拉菜单的Options
	 * @param array $arr 数据数组
	 * @param string $select 选中的下拉框的值
	 */
	function getOptionsString($arr, $select) {
		$str = "";
		reset($arr);
		foreach($arr as $key=>$val){
			$str .= "<option value=\"" . $key . "\"";
			if (strcmp($key, $select) == 0) $str .= " selected";
			$str .= ">" . $val . "</option>\n";
		}
		
		return $str;
	}
	
	/**
	  *	makeFile
	  * 生成文件
	  * @pram string $file_name	路径文件名
	  * @pram string $content	内容
	  */
	function makeFile($file_name, $content = ""){ 
		//目录不存在就创建 
		if (!file_exists (dirname($file_name))) { 
			$this->createDir($file_name);
		} 

		if(!$fp = fopen($file_name, "w")){ 
			echo "open file unsuccessful！\n"; 
			return false; 
		} 

		if(!fwrite($fp, $content)){ 
			echo "write file unsuccessful！\n"; 
			fclose($fp); 
			return false; 
		} 

		fclose($fp);
		chmod($file_name,0666);
		return true;
	}

	/**
	  *	createDir
	  * 逐级创建目录
	  * @pram string $path
	  */
	function createDir($path) {
		$path = dirname($path);
		$path_arr = explode("/", $path);
		$p_str = "";

		foreach($path_arr as $key => $val) {
			$p_str .= $val."/"; 
			if(!file_exists($p_str)) {
				mkdir ($p_str, 0777);	
			}	
		}	
	}
	
	/**
	 * encode
	 * 自定义编码函数
	 * @param string
	 * @return String
	 */
	function encode($str) {
		return urlencode(base64_encode($str));
	}
	
	/**
	 * decode
	 * 自定义解码函数
	 * @param string
	 * @return String
	 */	
	function decode($str) {
		return base64_decode(urldecode($str));
	}

	/**
	 * fielUpLoad
	 * 文件上传
	 * @param $srcFile	 
	 * @param $path
	 */
	function fileUpLoad($srcFile, $path){
		if (!file_exists (dirname($path))) { 
			$this->createDir($path);
		} 
		//return move_uploaded_file($srcFile, $path);
		return copy($srcFile, $path);
	}
	
	/**
	 * delFile
	 * @param String $filePath
	 */
	function delFile($filePath){
		if(file_exists($filePath)){
			unlink($filePath);
		}
	}	
}
?>